package com.kehao.leetcode.basic.sortAndSearch;

import org.junit.Test;

public class SortAndSearchSolution1 {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] nums3 = new int[m];
        for (int i = 0; i < m; i++) {
            nums3[i] = nums1[i];
        }
        int i2=0,i3=0;
        int i1 = 0;
        while (i2<n&&i3<m){
            if(nums2[i2]<nums3[i3]){
                nums1[i1]=nums2[i2];
                i1++;
                i2++;
            }else if(nums2[i2]>nums3[i3]){
                nums1[i1]=nums3[i3];
                i1++;
                i3++;
            }else {
                nums1[i1]=nums2[i2];
                i1++;
                i2++;
                nums1[i1]=nums3[i3];
                i1++;
                i3++;
            }
        }
        while (i2<n){
            nums1[i1]=nums2[i2];
            i1++;
            i2++;
        }
        while (i3<m){
            nums1[i1]=nums3[i3];
            i1++;
            i3++;
        }
    }

    @Test
    public void test01(){
        int [] nums1 = {1,2,3,0,0,0};
        int m = 3;
        int[] nums2 = {2,5,6};
        int n = 3;
        merge(nums1,m,nums2,n);
        for (int i : nums1) {
            System.out.print(i);
        }
    }

    @Test
    public void test02(){
        int [] nums1 = {1};
        int m = 1;
        int[] nums2 = {};
        int n = 0;
        merge(nums1,m,nums2,n);
        for (int i : nums1) {
            System.out.print(i);
        }
    }
}
